package com.healthcloud.controller;

import com.healthcloud.service.SysGeneratorService;
import com.healthcloud.utils.PageParam;
import com.healthcloud.xss.XssHttpServletRequestWrapper;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成器
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年12月19日 下午9:12:58
 */
@ApiIgnore
@Controller
@RequestMapping("/sys/generator")
public class SysGeneratorController {
	@Autowired
	private SysGeneratorService sysGeneratorService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
/*	@RequiresPermissions("sys:generator:list")*/
	public String list(){

		return "gen/genMgr";
//		//查询列表数据
//		Query query = new Query(params);
//		List<Map<String, Object>> list = sysGeneratorService.queryList(query);
//		int total = sysGeneratorService.queryTotal(query);
//
//		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
//
//		return R.ok().put("page", pageUtil);
	}


/*	@RequiresPermissions("sys:generator:list")*/
	@RequestMapping(value = "/searchGen")
	@ResponseBody
	public Map<String, Object> searchGen(@RequestParam Map<String, Object> params,@ModelAttribute("pageParam")PageParam pageParam){

		List<Map<String, Object>> list = sysGeneratorService.queryList(params);
		int total = sysGeneratorService.queryTotal(params);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dataList", list);
		map.put("iTotalRecords", total);
		map.put("iTotalDisplayRecords", total);
		map.put("sEcho", pageParam.getsEcho());

		return map;

	}
	
	/**
	 * 生成代码
	 */
	@RequestMapping("/code")
//	@RequiresPermissions("sys:generator:code")
	public void code(HttpServletRequest request, HttpServletResponse response){
		try {
			String[] tableNames = new String[]{};
			//获取表名，不进行xss过滤
			HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
			String tables = orgRequest.getParameter("tables");
			//tableNames = JSON.parseArray(tables).toArray(tableNames);
			tableNames = tables.split(",");

			byte[] data = sysGeneratorService.generatorCode(tableNames);

			response.reset();
			response.setHeader("Content-Disposition", "attachment; filename=\"healthcloud.zip\"");
			response.addHeader("Content-Length", "" + data.length);
			response.setContentType("application/octet-stream; charset=UTF-8");

			IOUtils.write(data, response.getOutputStream());
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}
